﻿using UnityEngine;

namespace BToolkit
{
    [ExecuteInEditMode]
    public class RenderQueue : MonoBehaviour
    {
        public int renderQueue = 3001;
        UnityEngine.Renderer[] _renderers;

        void Start()
        {
            if (Application.isPlaying)
            {
                Execute();
            }
        }

        void Update()
        {
            if (Application.isEditor)
            {
                Execute();
            }
        }

        void Execute()
        {
            if (_renderers == null)
            {
                _renderers = GetComponentsInChildren<UnityEngine.Renderer>();
            }
            for (int i = 0; i < _renderers.Length; i++)
            {
                if (_renderers[i] != null && _renderers[i].sharedMaterial != null)
                {
                    _renderers[i].material.renderQueue = renderQueue;
                }
            }
        }
    }
}